package com.lhz.framework.security;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.util.List;

/**
 * @Author: LiHuaZhi
 * @Date: 2021/11/18 17:41
 * @Version: 1.0
 **/
public class HolderInfoManager {
    private static final ThreadLocal<String> USER_ID_HOLDER = new ThreadLocal<>();
    private static final ThreadLocal<String> USER_NAME_HOLDER = new ThreadLocal<>();
    private static final ThreadLocal<String> TOKEN_HOLDER = new ThreadLocal<>();
    private static final ThreadLocal<HolderUserInfo> USER_INFO_HOLDER = new ThreadLocal<>();
    private static final ThreadLocal<HttpServletRequest> REQUEST_HOLDER = new ThreadLocal<>();

    private static final ThreadLocal<HttpServletResponse> RESPONSE_HOLDER = new ThreadLocal<>();

    private static final ThreadLocal<Integer> DEPT_ID = new ThreadLocal<>();
    private static final ThreadLocal<Integer> DATA_SCOPE = new ThreadLocal<>();
    private static final ThreadLocal<List<Integer>> DEPT_IDS = new ThreadLocal<>();

    public static void setDeptId(Integer deptId) {
        HolderInfoManager.DEPT_ID.set(deptId);
    }

    public static void setDataScope(Integer dataScope) {
        HolderInfoManager.DATA_SCOPE.set(dataScope);
    }

    public static void setDeptIds(List<Integer> deptIds) {
        HolderInfoManager.DEPT_IDS.set(deptIds);
    }


    public static Integer getDeptId() {
        return DEPT_ID.get();
    }

    public static Integer getDataScope() {
        return DATA_SCOPE.get();
    }

    public static List<Integer> getDeptIds() {
        return DEPT_IDS.get();
    }

    public static String getUserId() {
        return USER_ID_HOLDER.get();
    }

    public static String getUserName() {
        return USER_NAME_HOLDER.get();
    }

    public static String getToken() {
        return TOKEN_HOLDER.get();
    }

    public static HolderUserInfo getUserInfo() {
        return USER_INFO_HOLDER.get();
    }

    public static HttpServletRequest getCurrentRequest() {
        return REQUEST_HOLDER.get();
    }

    public static ThreadLocal<HttpServletResponse> getResponseHolder() {
        return RESPONSE_HOLDER;
    }

    public static void addRequest(HttpServletRequest request) {
        REQUEST_HOLDER.set(request);
    }


    public static void addResponse(HttpServletResponse response) {
        RESPONSE_HOLDER.set(response);
    }

    public static void addToken(String token) {
        TOKEN_HOLDER.set(token);
    }

    public static void addUserId(String userId) {
        USER_ID_HOLDER.set(userId);
    }

    public static void addUserInfo(HolderUserInfo userInfo) {
        USER_INFO_HOLDER.set(userInfo);
    }

    public static void addUserName(String userId) {
        USER_NAME_HOLDER.set(userId);
    }

    public static void remove() {
        REQUEST_HOLDER.remove();
        RESPONSE_HOLDER.remove();
        TOKEN_HOLDER.remove();
        USER_ID_HOLDER.remove();
        USER_NAME_HOLDER.remove();
    }
}